<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="书籍名" prop="bookName">
        <el-input
          v-model="queryParams.bookName"
          placeholder="请输入书籍名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍类型" prop="bookType">
        <el-select v-model="queryParams.bookType" placeholder="请选择书籍类型" clearable>
          <el-option
            v-for="dict in dict.type.zhx_book_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入创建人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入创建人用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接龙类型" prop="jielongType">
        <el-select v-model="queryParams.jielongType" placeholder="请选择接龙类型" clearable>
          <el-option
            v-for="dict in dict.type.zhx_jielong_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接龙层级数" prop="jielongLevel">
        <el-input
          v-model="queryParams.jielongLevel"
          placeholder="请输入接龙层级数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接龙时间" prop="jielongTime">
        <el-input
          v-model="queryParams.jielongTime"
          placeholder="请输入接龙时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点赞数" prop="likeNum">
        <el-input
          v-model="queryParams.likeNum"
          placeholder="请输入点赞数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论数" prop="commentNum">
        <el-input
          v-model="queryParams.commentNum"
          placeholder="请输入评论数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接龙数" prop="jielongNum">
        <el-input
          v-model="queryParams.jielongNum"
          placeholder="请输入接龙数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收藏数" prop="collectNum">
        <el-input
          v-model="queryParams.collectNum"
          placeholder="请输入收藏数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订阅数" prop="subscribeNum">
        <el-input
          v-model="queryParams.subscribeNum"
          placeholder="请输入订阅数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阅读数" prop="readNum">
        <el-input
          v-model="queryParams.readNum"
          placeholder="请输入阅读数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人气值" prop="popularity">
        <el-input
          v-model="queryParams.popularity"
          placeholder="请输入人气值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.zhx_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ZhxAdmin:ZhxBook:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ZhxAdmin:ZhxBook:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ZhxAdmin:ZhxBook:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ZhxAdmin:ZhxBook:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ZhxBookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="书籍id" align="center" prop="bookId" />
      <el-table-column label="书籍名" align="center" prop="bookName" />
      <el-table-column label="书籍描述" align="center" prop="bookDesc" />
      <el-table-column label="书籍类型" align="center" prop="bookType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhx_book_type" :value="scope.row.bookType"/>
        </template>
      </el-table-column>
      <el-table-column label="书籍封面" align="center" prop="bookCover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.bookCover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人id" align="center" prop="userId" />
      <el-table-column label="创建人用户名" align="center" prop="userName" />
      <el-table-column label="创建人头像" align="center" prop="userAvatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.userAvatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="接龙类型" align="center" prop="jielongType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhx_jielong_type" :value="scope.row.jielongType"/>
        </template>
      </el-table-column>
      <el-table-column label="接龙层级数" align="center" prop="jielongLevel" />
      <el-table-column label="接龙时间" align="center" prop="jielongTime" />
      <el-table-column label="点赞数" align="center" prop="likeNum" />
      <el-table-column label="评论数" align="center" prop="commentNum" />
      <el-table-column label="接龙数" align="center" prop="jielongNum" />
      <el-table-column label="收藏数" align="center" prop="collectNum" />
      <el-table-column label="订阅数" align="center" prop="subscribeNum" />
      <el-table-column label="阅读数" align="center" prop="readNum" />
      <el-table-column label="人气值" align="center" prop="popularity" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhx_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ZhxAdmin:ZhxBook:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ZhxAdmin:ZhxBook:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改作品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="书籍名" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入书籍名" />
        </el-form-item>
        <el-form-item label="书籍描述">
          <editor v-model="form.bookDesc" :min-height="192"/>
        </el-form-item>
        <el-form-item label="书籍类型" prop="bookType">
          <el-select v-model="form.bookType" placeholder="请选择书籍类型">
            <el-option
              v-for="dict in dict.type.zhx_book_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="书籍封面" prop="bookCover">
          <image-upload v-model="form.bookCover"/>
        </el-form-item>
        <el-form-item label="创建人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入创建人id" />
        </el-form-item>
        <el-form-item label="创建人用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入创建人用户名" />
        </el-form-item>
        <el-form-item label="创建人头像" prop="userAvatar">
          <image-upload v-model="form.userAvatar"/>
        </el-form-item>
        <el-form-item label="接龙类型" prop="jielongType">
          <el-select v-model="form.jielongType" placeholder="请选择接龙类型">
            <el-option
              v-for="dict in dict.type.zhx_jielong_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接龙层级数" prop="jielongLevel">
          <el-input v-model="form.jielongLevel" placeholder="请输入接龙层级数" />
        </el-form-item>
        <el-form-item label="接龙时间" prop="jielongTime">
          <el-input v-model="form.jielongTime" placeholder="请输入接龙时间" />
        </el-form-item>
        <el-form-item label="点赞数" prop="likeNum">
          <el-input v-model="form.likeNum" placeholder="请输入点赞数" />
        </el-form-item>
        <el-form-item label="评论数" prop="commentNum">
          <el-input v-model="form.commentNum" placeholder="请输入评论数" />
        </el-form-item>
        <el-form-item label="接龙数" prop="jielongNum">
          <el-input v-model="form.jielongNum" placeholder="请输入接龙数" />
        </el-form-item>
        <el-form-item label="收藏数" prop="collectNum">
          <el-input v-model="form.collectNum" placeholder="请输入收藏数" />
        </el-form-item>
        <el-form-item label="订阅数" prop="subscribeNum">
          <el-input v-model="form.subscribeNum" placeholder="请输入订阅数" />
        </el-form-item>
        <el-form-item label="阅读数" prop="readNum">
          <el-input v-model="form.readNum" placeholder="请输入阅读数" />
        </el-form-item>
        <el-form-item label="人气值" prop="popularity">
          <el-input v-model="form.popularity" placeholder="请输入人气值" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.zhx_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listZhxBook, getZhxBook, delZhxBook, addZhxBook, updateZhxBook } from "@/api/ZhxAdmin/ZhxBook";

export default {
  name: "ZhxBook",
  dicts: ['zhx_jielong_type', 'zhx_status', 'zhx_book_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 作品管理表格数据
      ZhxBookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: null,
        bookDesc: null,
        bookType: null,
        bookCover: null,
        userId: null,
        userName: null,
        userAvatar: null,
        jielongType: null,
        jielongLevel: null,
        jielongTime: null,
        likeNum: null,
        commentNum: null,
        jielongNum: null,
        collectNum: null,
        subscribeNum: null,
        readNum: null,
        popularity: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bookName: [
          { required: true, message: "书籍名不能为空", trigger: "blur" }
        ],
        bookDesc: [
          { required: true, message: "书籍描述不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询作品管理列表 */
    getList() {
      this.loading = true;
      listZhxBook(this.queryParams).then(response => {
        this.ZhxBookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookId: null,
        bookName: null,
        bookDesc: null,
        bookType: null,
        bookCover: null,
        userId: null,
        userName: null,
        userAvatar: null,
        jielongType: null,
        jielongLevel: null,
        jielongTime: null,
        likeNum: null,
        commentNum: null,
        jielongNum: null,
        collectNum: null,
        subscribeNum: null,
        readNum: null,
        popularity: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加作品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bookId = row.bookId || this.ids
      getZhxBook(bookId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改作品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bookId != null) {
            updateZhxBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZhxBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bookIds = row.bookId || this.ids;
      this.$modal.confirm('是否确认删除作品管理编号为"' + bookIds + '"的数据项？').then(function() {
        return delZhxBook(bookIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ZhxAdmin/ZhxBook/export', {
        ...this.queryParams
      }, `ZhxBook_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
